import {
  Body,
  Controller,
  Get,
  Post,
  Put,
  Req,
  UseGuards,
} from '@nestjs/common';
import {
  ApiBearerAuth,
  ApiOkResponse,
  ApiOperation,
  ApiTags,
} from '@nestjs/swagger';
import { ResponseData } from '../../common/interfaces/result.interface';
import { RoleDct } from '../../decorators/role.dct.decorator';
import { JwtAuthGuard } from '../user/guards/jwt-auth.guard';
import { RoleGuard } from '../user/guards/role.guard';
import { UserRoles } from '../user/user.entity';
import { CreateMenuDto } from './dto/create-menu.dto';
import { UpdateMenuDto } from './dto/update-menu.dto';
import { MenuService } from './menu.service';

@Controller('menu')
@ApiTags('菜单')
@ApiBearerAuth()
@UseGuards(JwtAuthGuard, RoleGuard)
@RoleDct(UserRoles.admin, UserRoles.editor, UserRoles.super)
export class MenuController {
  constructor(private readonly menuService: MenuService) {}

  @Get()
  @RoleDct(UserRoles.super, UserRoles.admin)
  async findAll() {
    return await this.menuService.findAll();
  }

  @Post()
  async create(@Body() data: CreateMenuDto) {
    console.log('data', data);
    // return 'ok'
    return await this.menuService.create(data);
  }

  @Put('update')
  @ApiOperation({ summary: '根据菜单id更新菜单信息信息' })
  @ApiOkResponse({ description: '根据菜单id更新菜单信息', type: UpdateMenuDto })
  async update(@Body() menuData: UpdateMenuDto): Promise<ResponseData> {
    return await this.menuService.update(menuData);
  }

  @Get('getTree')
  async getTree(@Req() req): Promise<ResponseData> {
    console.log('request', req.user);
    const { user } = req;
    return await this.menuService.getTree(user);
  }
}
